/*
** get_password.c for get_passwd in /home/comet_a//afs/local/sys_unix/my_select/ex_2
** 
** Made by alexandre comet
** Login   <comet_a@epitech.net>
** 
** Started on  Fri Jan 13 11:13:12 2012 alexandre comet
** Last update Fri Jan 13 12:57:30 2012 alexandre comet
*/

#include	<termios.h>
#include	<unistd.h>
#include	<stdlib.h>
#include	<stdio.h>

void		my_putchar(char c)
{
  write(1, &c, 1);
}

void		my_putstr(char *str)
{
  int		i;

  i = 0;
  while (str[i])
    my_putchar(str[i++]);
}

int		raw_mode(struct termios *t)
{
  if (tcgetattr(0, t) == -1)
    {
      fprintf(stderr, "tcgetattr failed\n");
      return (1);
    }
  t->c_lflag &= ~ECHO;//desactivation
  t->c_lflag &= ~ICANON;
  t->c_cc[VMIN] = 1;
  t->c_cc[VTIME] = 0;
  if (tcsetattr(0, TCSANOW, t) == -1)
    {
      fprintf(stderr, "tcsetattr failed\n");
      return (1);
    }
  return (0);
}

int		main()
{
  struct termios	t;
  int			i;
  int			r;
  char			*str;

  raw_mode(&t);
  i = 0;
  my_putstr("Enter password : ");
  while ((r = read(0, str, 1)) == 1)
    {
      my_putchar('*');
      if (*str == '\n')
	r = 0;
    }
  my_putstr("\nYour Password is : ");
  my_putstr(str);
  //t.c_lflag |= ECHO; //activation
  //t->c_lflag ^= ECHO;  == switch
  return (0);
}
